<template>
  <div class="ele-body">
    <el-card shadow="never">
      <!-- 搜索表单 -->
      <el-form
        :model="where"
        label-width="77px"
        class="ele-form-search"
        @keyup.enter.native="reload"
        @submit.native.prevent>
        <el-row>
          <el-col :md="4" :sm="12">
            <el-form-item label="起始日期:">
              <el-date-picker size="mini" v-model="where.riqiBegin" type="date" format='yyyy-MM-dd' value-format='yyyy-MM-dd' placeholder="选择日期" clearable class="ele-fluid"></el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :md="4" :sm="12">
            <el-form-item label="结束日期:">
              <el-date-picker size="mini" v-model="where.riqiEnd" type="date" format='yyyy-MM-dd' value-format='yyyy-MM-dd' placeholder="选择日期" clearable class="ele-fluid"></el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :lg="4" :md="12">
            <el-form-item label="SAP单号:">
              <el-input size="mini"
                        clearable
                        v-model="where.docno"
                        placeholder=""/>
            </el-form-item>
          </el-col>
          <el-col :lg="4" :md="12">
            <el-form-item label="板块:" label-width="80px">
              <el-select size="mini"
                         clearable
                         v-model="where.companycode"
                         placeholder="请选择板块"
                         class="ele-fluid">
                <el-option v-for="item in ssbkList" :label="item.name" :value="item.code" :key="item.code" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :lg="3" :md="12">
            <el-form-item label="箱型:" label-width="50px">
              <el-select size="mini"
                         clearable
                         v-model="where.boxflag"
                         placeholder="请选择"
                         class="ele-fluid">
                <el-option label="整箱" value="ZX"></el-option>
                <el-option label="拼箱" value="PX"></el-option>
                <el-option label="零箱" value="LX"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :lg="4" :md="12">
            <el-form-item label="药品:">
              <el-input size="mini"
                        clearable
                        v-model="where.itemno"
                        placeholder=""/>
            </el-form-item>
          </el-col>
          <el-col :lg="4" :md="12">
            <el-form-item label="批号:">
              <el-input size="mini"
                        clearable
                        v-model="where.vendorbatch"
                        placeholder=""/>
            </el-form-item>
          </el-col>
          <el-col :lg="4" :md="12">
            <el-form-item label="转出库位:">
              <el-input size="mini"
                        clearable
                        v-model="where.fromlocation"
                        placeholder=""/>
            </el-form-item>
          </el-col>
          <el-col :lg="4" :md="12">
            <el-form-item label="转入库位:" label-width="80px">
              <el-input size="mini"
                        clearable
                        v-model="where.tolocation"
                        placeholder=""/>
            </el-form-item>
          </el-col>
          <el-col :lg="4" :md="12">
            <el-form-item label="状态:" label-width="50px">
              <el-select size="mini"
                         clearable
                         v-model="where.status"
                         placeholder="请选择操作状态"
                         class="ele-fluid">
                <el-option label="已回写" value="已回写"></el-option>
                <el-option label="未回写" value="未回写"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :lg="4" :md="12">
            <div class="ele-form-actions" style="padding-top:4px">
              <el-button
                type="primary"
                icon="el-icon-search"
                class="ele-btn-icon"
                size="mini"
                @click="reload">查询
              </el-button>
              <el-button
                type="warning"
                icon="el-icon-download"
                class="ele-btn-icon"
                size="mini"
                v-if="permission.includes('sys:kdlgm:feedback')"
                @click="syncGM">获取
              </el-button>
              <el-button @click="reset" size="mini" class="ele-btn-icon">重置</el-button>
            </div>
          </el-col>
        </el-row>
      </el-form>
      <!-- 数据表格 -->
      <ele-pro-table
        ref="table"
        :where="where"
        :datasource="url"
        :columns="columns"
        :toolbar="false"
        :selection.sync="selection"
        :header-cell-style="{'text-align':'center'}"
        highlight-current-row
        @row-click="loadDetail"
        height="calc(100vh - 450px)">
        <!-- 表头工具栏 -->
        <template slot="toolbar" v-if="false">
          <el-button
            size="small"
            type="primary"
            icon="el-icon-plus"
            class="ele-btn-icon"
            @click="openEdit(null)"
            v-if="permission.includes('sys:kdlpurchasein:add')">添加
          </el-button>
          <el-button
            size="small"
            type="danger"
            icon="el-icon-delete"
            class="ele-btn-icon"
            @click="removeBatch"
            v-if="permission.includes('sys:kdlpurchasein:dall')">删除
          </el-button>
        </template>
        <!-- 操作列 -->
        <template slot="action" slot-scope="{row}">
          <el-link
            type="primary"
            :underline="false"
            icon="el-icon-upload"
            @click="openEdit(row)"
            v-if="permission.includes('sys:kdlz05:submitnc')">NC回写
          </el-link>
          <el-link
            type="success"
            :underline="false"
            icon="el-icon-document"
            @click="openLog(row)"
          >日志</el-link>
        </template>
      </ele-pro-table>
      <ele-pro-table
        ref="tableDetail"
        :where="where"
        :datasource="detailList"
        :columns="columnsDetail"
        :toolbar="false"
        :selection.sync="selection"
        :page-size="20"
        :need-page="false"
        :header-cell-style="{'text-align':'center'}"
        highlight-current-row
        height="220px">
        <!-- 表头工具栏 -->
        <template slot="toolbar" v-if="false">
          <el-button
            size="small"
            type="primary"
            icon="el-icon-plus"
            class="ele-btn-icon"
            @click="openEdit(null)"
            v-if="permission.includes('sys:kdlotherinoutb:add')">添加
          </el-button>
          <el-button
            size="small"
            type="danger"
            icon="el-icon-delete"
            class="ele-btn-icon"
            @click="removeBatch"
            v-if="permission.includes('sys:kdlotherinoutb:dall')">删除
          </el-button>
        </template>
        <!-- 操作列 -->
        <template slot="action" slot-scope="{row}">
          <el-link
            type="primary"
            :underline="false"
            icon="el-icon-upload"
            @click="openEdit(row)"
            v-if="permission.includes('sys:kdlgm:submitnc')">NC回写
          </el-link>
          <el-link
            type="primary"
            :underline="false"
            icon="el-icon-upload"
            @click="openFeedback(row)"
            v-if="permission.includes('sys:kdlgm:submitnc')">组织更新
          </el-link>
          <el-link
            type="success"
            :underline="false"
            icon="el-icon-document"
            @click="openLog(row)"
          >日志</el-link>
        </template>
      </ele-pro-table>
    </el-card>
    <!-- 编辑弹窗 -->
    <FeedbackEdit
      :data="current"
      :visible.sync="showFeedback"
      @done="reloadDetail"/>
    <LogList
      :dataItem="current"
      :visible.sync="showLog"
    />
  </div>
</template>

<script>
import { mapGetters } from "vuex";
import LogList from '../../../purchase/inbound/loglist';
import FeedbackEdit from './feedback-edit'
import setting from "@/config/setting";
export default {
  name: 'ExamKdlOtherInoutB',
  components: {LogList,FeedbackEdit},
  computed: {
    ...mapGetters(["permission"]),
  },
  data() {
    return {
      // 表格数据接口
      url: '/kdlgm/feedbacklist',
      // 表格列配置
      columns: [
        {
          columnKey: 'index',
          type: 'index',
          width: 45,
          align: 'center',
          fixed: "left"
        },
        {
          prop: 'orderdate',
          label: '单据日期',
          showOverflowTooltip: true,
          minWidth: 100,
          align: 'center'
        },
        {
          prop: 'docno',
          label: 'SAP单号',
          showOverflowTooltip: true,
          minWidth: 140,
          align: 'center'
        },
        {
          prop: 'orderno',
          label: '物料凭证号',
          showOverflowTooltip: true,
          minWidth: 140,
          align: 'center'
        },
        {
          prop: 'orgname',
          label: '事业部',
          showOverflowTooltip: true,
          minWidth: 110,
          align: 'left'
        },
        {
          prop: 'billsource',
          label: '来源',
          showOverflowTooltip: true,
          minWidth: 60,
          align: 'center'
        },
        {
          prop: 'plant',
          label: '工厂',
          showOverflowTooltip: true,
          minWidth: 60,
          align: 'center'
        },
        {
          prop: 'gmname',
          label: '移库人',
          showOverflowTooltip: true,
          minWidth: 80,
          align: 'center'
        },
        {
          prop: 'principalorderno',
          label: '信谊指令号',
          showOverflowTooltip: true,
          minWidth: 120,
          align: 'center'
        },
        {
          prop: 'doctype',
          label: '单据类型',
          showOverflowTooltip: true,
          minWidth: 80,
          align: 'center'
        },
        {
          prop: 'ordertypedesc',
          label: '业务类型',
          showOverflowTooltip: true,
          minWidth: 100,
          align: 'center'
        },
        {
          prop: 'syncTime',
          label: '同步时间',
          showOverflowTooltip: true,
          minWidth: 150,
          align: 'center'
        },
      ],
      columnsDetail: [
        {
          columnKey: 'index',
          type: 'index',
          width: 45,
          align: 'center',
          fixed: "left"
        },
        {
          prop: 'orderdate',
          label: '单据日期',
          showOverflowTooltip: true,
          minWidth: 100,
          align: 'center'
        },
        {
          prop: 'docno',
          label: 'SAP单号',
          showOverflowTooltip: true,
          minWidth: 140,
          align: 'center'
        },
        {
          prop: 'orgname',
          label: '事业部',
          showOverflowTooltip: true,
          minWidth: 110,
          align: 'left'
        },
        {
          prop: 'itemno',
          label: '药品编码',
          showOverflowTooltip: true,
          minWidth: 100,
          align: 'center'
        },
        {
          prop: 'comdescription',
          label: '通用名',
          showOverflowTooltip: true,
          minWidth: 120,
          align: 'left'
        },
        {
          prop: 'materialspec',
          label: '规格',
          showOverflowTooltip: true,
          minWidth: 160,
          align: 'left'
        },
        {
          prop: 'materialunit',
          label: '单位',
          showOverflowTooltip: true,
          minWidth: 60,
          align: 'center'
        },
        {
          prop: 'qty',
          label: '数量',
          showOverflowTooltip: true,
          minWidth: 80,
          align: 'center'
        },
        {
          prop: 'vendorbatch',
          label: '批号',
          showOverflowTooltip: true,
          minWidth: 160,
          align: 'left'
        },
        {
          prop: 'fromlocation',
          label: '转出库位',
          showOverflowTooltip: true,
          minWidth: 80,
          align: 'center'
        },
        {
          prop: 'frombindesc',
          label: '库位名称',
          showOverflowTooltip: true,
          minWidth: 110,
          align: 'left'
        },
        {
          prop: 'tolocation',
          label: '转入库位',
          showOverflowTooltip: true,
          minWidth: 80,
          align: 'center'
        },
        {
          prop: 'tobindesc',
          label: '库位名称',
          showOverflowTooltip: true,
          minWidth: 110,
          align: 'left'
        },
        {
          prop: 'boxflag',
          label: '箱型',
          showOverflowTooltip: true,
          minWidth: 70,
          align: 'center'
        },
        {
          prop: 'boxno',
          label: '箱号',
          showOverflowTooltip: true,
          minWidth: 220,
          align: 'center'
        },
        {
          prop: 'materialtype',
          label: '药品类型',
          showOverflowTooltip: true,
          minWidth: 160,
          align: 'left'
        },
        {
          prop: 'lregistno',
          label: '批准文号',
          showOverflowTooltip: true,
          minWidth: 140,
          align: 'left'
        },
        {
          prop: 'materialform',
          label: '剂型',
          showOverflowTooltip: true,
          minWidth: 80,
          align: 'left',
        },
        {
          prop: 'manufacturername',
          label: '生产企业',
          showOverflowTooltip: true,
          minWidth: 180,
          align: 'left'
        },
        {
          prop: 'batchno',
          label: 'SAP批次',
          showOverflowTooltip: true,
          minWidth: 100,
          align: 'center'
        },
        {
          prop: 'ncbatch',
          label: 'NC批次',
          showOverflowTooltip: true,
          minWidth: 160,
          align: 'left'
        },
        {
          prop: 'billsource',
          label: '来源',
          showOverflowTooltip: true,
          minWidth: 100,
          align: 'center'
        },
        {
          prop: 'plant',
          label: '工厂',
          showOverflowTooltip: true,
          minWidth: 100,
          align: 'center'
        },
        {
          prop: 'gmname',
          label: '移库人',
          showOverflowTooltip: true,
          minWidth: 100,
          align: 'center'
        },
        {
          prop: 'status',
          label: '状态',
          showOverflowTooltip: true,
          minWidth: 100,
          align: 'center'
        },
        // {
        //   prop: 'orderno',
        //   label: 'SAP物料凭证号',
        //   showOverflowTooltip: true,
        //   minWidth: 120,
        //   align: 'center'
        // },
        {
          prop: 'principalorderno',
          label: '信谊指令号',
          showOverflowTooltip: true,
          minWidth: 120,
          align: 'center'
        },
        {
          prop: 'doctype',
          label: '单据类型',
          showOverflowTooltip: true,
          minWidth: 100,
          align: 'center'
        },
        {
          prop: 'ordertypedesc',
          label: '业务类型',
          showOverflowTooltip: true,
          minWidth: 100,
          align: 'center'
        },
        // {
        //   prop: 'sn',
        //   label: 'SN',
        //   showOverflowTooltip: true,
        //   minWidth: 100,
        //   align: 'center'
        // },
        {
          columnKey: 'action',
          label: '操作',
          width: 250,
          align: 'center',
          resizable: false,
          slot: 'action',
          fixed: "right"
        }
      ],
      // 表格搜索条件
      where: {transtype:'trans',riqiBegin:setting.getFromDate(),riqiEnd:new Date()},
      // 表格选中数据
      selection: [],
      //移库明细列表
      detailList:[],
      ssbkList:[],
      // 当前编辑数据
      current: null,
      // 是否显示编辑弹窗
      showEdit: false,
      showLog:false,
      showFeedback:false,
    };
  },
  mounted() {
    this.getSsbkList();
  },
  methods: {
    async loadDetail(row){
      var res = await this.$http.get('/kdlgm/detail?orderno='+row.orderno);
      if(res.status==200)
      {
        this.detailList = res.data.data;
      }
    },
    async reloadDetail(){
      var res = await this.$http.get('/kdlgm/detail?orderno='+this.current.orderno);
      if(res.status==200)
      {
        this.detailList = res.data.data;
      }
    },

    /* 获取所属版块 */
    async getSsbkList(){
      var res = await this.$http.get('/dictdata/index?dictId=19&page=1&limit=1000');
      if(res.status==200)
      {
        this.ssbkList = res.data.data.records;
      }

    },
    /* 刷新表格 */
    reload() {
      this.$refs.table.reload({where: this.where});
    },
    /* 重置搜索 */
    reset() {
      this.where = {transtype:'trans'};
      this.reload();
    },
    syncGM(){
      const loading = this.$loading({lock: true});
      this.$http.post('/kdlgm/syncgm', {}).then(res => {
        loading.close();
        if (res.data.code === 0) {
          this.$message.success(res.data.msg);
          this.reload();
        } else {
          this.$message.error(res.data.msg);
        }
      }).catch(e => {
        loading.close();
        this.$message.error(e.message);
      });

    },
    openLog(row){
      this.current = row;
      this.current.chukNo = row.orderno;
      //console.log(this.current);
      this.showLog = true;
    },

    openFeedback(row){
      this.current = row;
      //console.log(this.current);
      this.showFeedback = true;
    },
    /* 显示编辑 */
    openEdit(row) {
      this.$confirm('确定要将选中的移库记录回写至NC吗?', '提示', {
        type: 'warning'
      }).then(() => {
        const loading = this.$loading({lock: true});
        this.$http.post('/kdl/submitWhstrans/'+ row.orderno).then(res => {
          loading.close();
          if (res.data.code === 0) {
            this.$message.success(res.data.msg);
            this.reload();
          } else {
            this.$message.error(res.data.msg);
          }
        }).catch(e => {
          loading.close();
          this.$message.error(e.message);
        });
      }).catch(() => {
      });
      // this.current = row;
      // this.showEdit = true;
    },    /* 删除 */
    remove(row) {
      const loading = this.$loading({lock: true});
      this.$http.delete('/kdlotherinoutb/delete', {id: row.id}).then(res => {
        loading.close();
        if (res.data.code === 0) {
          this.$message.success(res.data.msg);
          this.reload();
        } else {
          this.$message.error(res.data.msg);
        }
      }).catch(e => {
        loading.close();
        this.$message.error(e.message);
      });
    },
    /* 批量删除 */
    removeBatch() {
      if (!this.selection.length) {
        this.$message.error('请至少选择一条数据');
        return;
      }
      this.$confirm('确定要删除选中的其他出入库明细吗?', '提示', {
        type: 'warning'
      }).then(() => {
        const loading = this.$loading({lock: true});
        this.$http.delete('/kdlotherinoutb/delete', {id: this.selection.map(d => d.id)}).then(res => {
          loading.close();
          if (res.data.code === 0) {
            this.$message.success(res.data.msg);
            this.reload();
          } else {
            this.$message.error(res.data.msg);
          }
        }).catch(e => {
          loading.close();
          this.$message.error(e.message);
        });
      }).catch(() => {
      });
    },
  }
}
</script>

<style scoped>
.ele-form-search{
  margin-top: -15px ;
  margin-bottom: -5px;
}
.el-form-item {
  margin-bottom: -5px !important;
}
</style>
